package com.chen.springcloud.controller;

import com.chen.springcloud.entity.Dept;
import com.chen.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName
 * @Description:    提供(者)restful 服务
 * @Author:
 * @Date: Created in 16:27 2022/5/22
 */
@RestController /* @Controller + @ResponseBody 返回json数据不需要在方法前面加@ResponseBody注解了，但使用@RestController这个注解，就不能返回jsp,html页面，视图解析器无法解析jsp,html页面*/
@RequestMapping("/deptProvider")
public class DeptProviderController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private DiscoveryClient discoveryClient;    //获取一些配置信息，获取具体的微服务

    /**
     *  保存部门
     * @param dept
     * @return
     */
    @PostMapping("/saveDeptInfo")
    public Boolean saveDeptInfo(@RequestBody Dept dept){
        return deptService.saveDeptInfo(dept);
    }

    /**
     *  通过id 查询部门
     * @param id
     * @return
     */
    @GetMapping("/queryDeptById/{id}")
    public Dept queryDeptById(@PathVariable("id") Integer id){
        return deptService.queryDeptById(id);
    }

    /**
     *  查询所有部门
     * @return
     */
    @GetMapping("/queryDeptAll")
    public List<Dept> queryDeptAll(){
        return deptService.queryDeptAll();
    }

    /**
     *  用于团队协作时，发现服务的一些具体信息
     * @return
     */
    @GetMapping("/discovery")
    public Object discovery(){
        //获取微服务列表的清单
        List<String> servicesList = discoveryClient.getServices();
        System.out.println("servicesList==>"+servicesList);

        //通过具体的微服务的id 获取微服务的信息  applicationName
        List<ServiceInstance> instancesList = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instancesList) {
            System.out.println(
                    instance.getHost()+"\t"+
                    instance.getPort()+"\t"+
                    instance.getUri()+"\t"+
                    instance.getServiceId()+"\t"
                    );
        }
        return this.discoveryClient;
    }

}
